package docxtoexcel;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.*;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class DocxToExcel {
    public static void main(String[] args) throws Exception {
        /*Files.list(Paths.get("E:\\job\\tmp\\docx")).forEach(path -> {
            try {
                System.out.println(path.toFile().getAbsolutePath());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });*/
//        List<InterfaceRow> interfaceRows = searchWordDocX("E:\\job\\tmp\\docx\\45G协同智能关断接口文档.docx");
//        List<InterfaceRow> interfaceRows = searchWordDocX("E:\\job\\tmp\\docx\\云GIS-5G基础数据支撑-1个接口文档V1_20210122.docx");
//        List<InterfaceRow> interfaceRows = searchWordDocX("E:\\job\\tmp\\docx\\基础数据支撑.docx");
//        List<InterfaceRow> interfaceRows = searchWordDocX("E:\\job\\tmp\\docx\\基础数据支撑1.docx");
//        List<InterfaceRow> interfaceRows = searchWordDocX("E:\\job\\tmp\\docx\\物理站共址站计算.docx");
//        List<InterfaceRow> interfaceRows = searchWordDocX("E:\\job\\tmp\\docx\\物联网诊断口袋接口文档.docx");
//        List<InterfaceRow> interfaceRows = searchWordDocX("E:\\job\\tmp\\docx\\集中监控节假日及指标下钻.docx");
//        List<InterfaceRow> interfaceRows = searchWordDocX("E:\\job\\tmp\\docx\\集中质量分析-节假日-整体无线指标、机场、火车站、汽车站、特殊场景、top小区自定义、农村、个性化需求.docx");
//        List<InterfaceRow> interfaceRows = searchWordDocX("E:\\job\\tmp\\docx\\集中质量分析-节假日-景区-整体指标（省）及5A景区 .docx");
//        List<InterfaceRow> interfaceRows = searchWordDocX("E:\\job\\tmp\\docx\\集中质量分析-节假日-高速公路-地市指标 .docx");
//        List<InterfaceRow> interfaceRows = searchWordDocX("E:\\job\\tmp\\docx\\集中质量分析-节假日-高速公路-整体指标（省）及线路指标 .docx");
//        List<InterfaceRow> interfaceRows = searchWordDocX("E:\\job\\tmp\\docx\\集中质量分析-节假日-高铁-整体指标（省）及线路指标 .docx");
        List<InterfaceRow> interfaceRows = searchWordDocX("E:\\job\\tmp\\docx\\集中质量分析-节假日分析-场景下钻 .docx");
        for (InterfaceRow interfaceRow : interfaceRows) {
            System.out.println(interfaceRow);
        }
        ExcelUtil.write(interfaceRows);
    }

    public static List<InterfaceRow> searchWordDocX(String filePath) throws Exception {
        //读取文件路径
        OPCPackage opcPackage;
        opcPackage = POIXMLDocument.openPackage(filePath);
        XWPFDocument xwpf = new XWPFDocument(opcPackage);

        List<XWPFParagraph> paragraphs = xwpf.getParagraphs();
        List<XWPFTable> tables = xwpf.getTables();
        int i1 = 0;
        List<InterfaceRow> interfaceRows = new ArrayList<>();
        for (int i = 0; i < paragraphs.size(); i++) {
            String text = paragraphs.get(i).getText();
            if (text != null && text.contains("微服务管控平台的请求服务编码为")) {
                String serviceCode = paragraphs.get(i + 1).getText();
                System.out.println();
                System.out.println("------------------------------------------");
                System.out.println();
                System.out.println(serviceCode);

                String apiDescription = "";
                for (int j = i; j >= 0; j--) {
                    apiDescription = paragraphs.get(j).getText();
                    if (apiDescription != null && apiDescription.endsWith("接口")) {
                        apiDescription = apiDescription.replaceAll("[0-9]*\\.[0-9]*", "");
                        System.out.println(apiDescription);
                        break;
                    }
                }
                for (; i1 < tables.size(); i1++) {
                    XWPFTable table = tables.get(i1);
                    if (table.getText().contains("字段英文名")) {
                        System.out.println("-------------------入参-------------------");
                        List<XWPFTableRow> rows = table.getRows();
                        for (int i2 = 1; i2 < rows.size(); i2++) {
                            XWPFTableRow row = rows.get(i2);
                            List<XWPFTableCell> tableCells = row.getTableCells();
                            if (tableCells.size() == 5) {
                                InterfaceRow interfaceRow = new InterfaceRow();
                                interfaceRow.setApiCode(serviceCode);
                                interfaceRow.setApiDescription(apiDescription);
                                interfaceRow.setParamType("IN");
                                setInterfaceRow(interfaceRow, tableCells);
                                interfaceRows.add(interfaceRow);
                                printTableCells(tableCells);
                                System.out.print(",");
                            }
                            if (tableCells.size() < 5) {
                                for (XWPFTableCell cell : tableCells) {
//                                    System.out.print(cell.getText() + " ");
                                    for (XWPFTable cellTable : cell.getTables()) {
                                        for (XWPFTableRow tableRow : cellTable.getRows()) {
                                            InterfaceRow interfaceRow = new InterfaceRow();
                                            interfaceRow.setApiCode(serviceCode);
                                            interfaceRow.setApiDescription(apiDescription);
                                            interfaceRow.setParamType("IN");
                                            setInterfaceRow(interfaceRow, tableRow.getTableCells());
                                            interfaceRows.add(interfaceRow);
                                            printTableCells(tableRow.getTableCells());
                                            System.out.print(",");
                                        }
                                    }
                                }
                            }
                            System.out.println();
                        }
                    }
                    if (table.getText().contains("参数列表")) {
                        System.out.println("-------------------出参-------------------");
                        List<XWPFTableRow> rows = table.getRows();
                        for (int i2 = 1; i2 < rows.size(); i2++) {
                            XWPFTableRow row = rows.get(i2);
                            List<XWPFTableCell> tableCells = row.getTableCells();
                            if (tableCells.size() == 5) {
                                InterfaceRow interfaceRow = new InterfaceRow();
                                interfaceRow.setApiCode(serviceCode);
                                interfaceRow.setApiDescription(apiDescription);
                                interfaceRow.setParamType("OUT");
                                setInterfaceRow(interfaceRow, tableCells);
                                interfaceRows.add(interfaceRow);
                                printTableCells(tableCells);
                                System.out.print(",");
                            }
                            if (tableCells.size() < 5) {
                                for (XWPFTableCell cell : tableCells) {
//                                    System.out.print(cell.getText() + " ");
                                    for (XWPFTable cellTable : cell.getTables()) {
                                        for (XWPFTableRow tableRow : cellTable.getRows()) {
                                            InterfaceRow interfaceRow = new InterfaceRow();
                                            interfaceRow.setApiCode(serviceCode);
                                            interfaceRow.setApiDescription(apiDescription);
                                            interfaceRow.setParamType("OUT");
                                            setInterfaceRow(interfaceRow, tableRow.getTableCells());
                                            interfaceRows.add(interfaceRow);
                                            printTableCells(tableRow.getTableCells());
                                            System.out.print(",");
                                        }
                                    }
                                }
                            }
                            System.out.println();
                        }
                        int j = i1 + 1;
                        if (i1 + 1 < tables.size() && tables.get(i1 + 1).getText().contains("字段名为")) {
                            for (; j < tables.size(); j++) {
                                XWPFTable tab = tables.get(j);
                                String text1 = tab.getText();
                                if (text1 != null && text1.contains("字段名为")) {
                                    String tablename = text1.split("字段名为")[1].trim();
                                    List<XWPFTableRow> rows1 = tab.getRows().get(0).getTableCells().get(0).getTables().get(0).getRows();
                                    for (XWPFTableRow row : rows1) {
                                        InterfaceRow interfaceRow = new InterfaceRow();
                                        interfaceRow.setApiCode(serviceCode);
                                        interfaceRow.setApiDescription(apiDescription);
                                        interfaceRow.setParamType("OUT");
                                        setInterfaceRow(interfaceRow, row.getTableCells());
                                        interfaceRow.setParamDescription(row.getTableCells().get(4).getText() + tablename);
                                        interfaceRows.add(interfaceRow);
                                        printTableCells(row.getTableCells());
                                        System.out.print(",");
                                    }
                                }
                                System.out.println();
                            }
                        }
                        i1 = j;
                        break;
                    }
                }
            }
        }
        return interfaceRows;
    }

    private static void printTableCells(List<XWPFTableCell> tableCells) {
        for (XWPFTableCell tableCell : tableCells) {
            System.out.print(tableCell.getText() + " ");
        }
    }

    private static void setInterfaceRow(InterfaceRow interfaceRow, List<XWPFTableCell> tableCells) {
        if (tableCells.size() == 5) {
            interfaceRow.setParamCode(tableCells.get(0).getText());
            interfaceRow.setDataType(tableCells.get(1).getText());
            interfaceRow.setIsRequired(getIsRequired(tableCells.get(3).getText()));
            interfaceRow.setParamDescription(tableCells.get(4).getText());
        } else {
            interfaceRow.setParamCode(tableCells.get(0).getText());
            interfaceRow.setDataType(tableCells.get(1).getText());
            interfaceRow.setIsRequired("Y");
            interfaceRow.setParamDescription(tableCells.get(2).getText());
        }
    }

    private static String getIsRequired(String input) {
        String isRequired = "Y";
        if ("O".equalsIgnoreCase(input.trim())) {
            isRequired="N";
        }
        return isRequired;
    }
}
